#ifndef WIDGET_H
#define WIDGET_H
#include "settingdialog.h"
#include <QWidget>
#include <QLabel>
#include <QFile>
#include <QKeyEvent>
#include <QPixmap>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include <QImage>
#include <QFileInfo>
#include <iostream>
#include <pwm_show.h>
#include <cstring>
#include "datasheet.h"
#include <QRgb>
#include <QPushButton>
#include <QRadioButton>
#include <QFont>
#include <QSpinBox>
#include <QScrollBar>
#include "colorlabel.h"
#include <QMouseEvent>
#include <QWheelEvent>
#include <QGroupBox>
#include <QButtonGroup>
#include <QGridLayout>
#include <QIcon>
#include <QMainWindow>
#include <QCoreApplication>
#include <QApplication>
#include <QDesktopWidget>
#include <QFrame>
#include "uartprocessor.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include "motorshow.h"

#include "customsize.h"
#define maxScreen 100000
namespace Ui {
class Widget;
}

class Widget : public QMainWindow
{
    Q_OBJECT

public:

    explicit Widget(QMainWindow *parent = 0);
    ~Widget();

private:
    //原：80 60
    int Width = 80,Height = 60;
    QPixmap *blackPoint,*whitePoint,*redPoint;
    QPixmap pointArray[2];
    bool toSave = false;
    QString fileName;
    //以下是用到的按键
    QSpinBox *setPosition,*setSpeed;
    QLabel *speedLabel;
    QPushButton *Jump,*Play;
    QScrollBar *scrollPosition;
    int openMode;
    //以下函数处理原始数据
    QFile *sourceFile;
    static bool mapData[500][500];  //讲道理这个对bool的依赖完全没有必要
    QPixmap *mapPix[maxScreen];//支持播放2分钟的30帧动画
    static int mainData[16][maxScreen];
    static unsigned char bitData[maxScreen];

    int Sum;
    QLabel *sumLabel;
    void loadScreen(int n, QFile *F);
    int *isCross;//标明十字弯

    unsigned char * carStatus;
    //以下变量用于存储相关数据
    //int HL_a,HR_a,VL_a,VR_a;
    //int Direction,AimSpeed,CurrentSpeed,CurrentPWM;//舵机打角
    //int res[16];
    QGroupBox * dataGroup,*bitGroup,*processGroup,*mapGroup;
    QLabel *dataLabel[10][4],*crossLabel,*statusLabel,*deltaDataLabel1,*deltaDataLabel2,
    *deltaLabel1,*deltaLabel2;
    void setDataName();
    void setBitDataName();
    QGridLayout *dataLayout,*bitLayout,*processLayout,*mapLayout;
    QLabel *bitLabel[8],*bitDataLabel[8],*processLabel[8];
    QString bitName[8];
    //跳变的数量
    const int countChanges = 10;
    //状态数据
    static const int crossCount = 23, statusCount = 11;
    //图像状态
    const QString crossText[crossCount] = {"普通路径","出界混乱图像","单边沿","多边沿",
                                          "正入十字弯","单边沿拐点出弯","小s","一侧完全无效急弯",
                                          "障碍","远端无有效行急弯","直道","多边沿十字弯",
                                          "多边沿拐点","环形弯","fatal_ring","环形2",
                                          "环形3","多边沿直道","多边沿障碍",
                                          "多重长直道","单长直道","近端斑马线","远端斑马线"};
    //车状态
    const QString statusText[statusCount] = {"普通路径","直道","小s","障碍","急弯",
                                             "十字弯","环形弯","环形弯2","环形弯3",
                                             "其他","错误"};
    //表格显示
    DataSheet *datasheet;
    QFrame *dataframe;
    QGridLayout *datalayout;
    QWidget * dataSheetWidget;
    //以下变量用于处理图像的显示
    bool openStatus;
    int CurrentImage;
    QImage mapImg;
    QLabel imgLabel;
    QLabel *lineLabelx,*lineLabely;
    QRgb lAv,rAv,lIav,rIav,mAv;
    uartProcessor *Uarter;
    //以下变量用于处理设置窗口
    SettingDialog *settingDialog;
    QPushButton *settingButton;
    //当前变量名称
    QString varName[5];
    //以下变量用于显示相关信息
    QPushButton *pwmButton;
    long long int onlineData[100];
    PWM_Show *pwmshow;
    void pwmShow_init();
    QPushButton *defaultFile;
    void loadDefaultSetting();
    void loadControlDataSigned16(int *out,int n,QFile *tF);
    void loadControlDataUnsigned16(int *out,int n,QFile *tF);
    void loadControlDataUnsigned8(int *out,int n,QFile *tF);
    void enableButtons();
    void disableButtons();
    //以下为菜单栏相关变量和成员函数
    void setMenuBar();
    bool onlineModeStatus;
    QMenu *fileMenu,*toolMenu,*systemMenu;
    QAction *motorTool;
    MotorShow *motorShow;
    QAction *onlineModeMenu;
    QLabel *mousePos;


public slots:
    //void changeDataSheetSize();//改变数表的大小
    void openFile(void);
    void loadFile();
    void showMap(void);
    void setting();
    void showNext();
    void playStart();
    void playEnd();
    //打开主路图
    void openMainRoadFile();
    //void showLast();
    void JumpTo();
    void toScroll(int t);
    void changeSettings();//此槽函数用于更改设置。
    void showPWM();
    void showCustomSize();
    void openDefaultFile();
    void enterOnlineMode();
    void exitOnlineMode();
    void showOnlineMap();
    void showMotorTool();//显示电机工具
    //两个菜单栏
    void openEdgeFile();
    void openBitFile();
    void showAboutInfo();
    void openZipFile();
protected:
    void timerEvent(QTimerEvent *Event);
    void mousePressEvent(QMouseEvent *Event);
    void mouseMoveEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
    int timer_ID;
};
#endif // WIDGET_H
